<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>contractList</title>
    <link href="../style/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="../style/summernote.css" rel="stylesheet">
    <link href="../style/summernote-bs3.css" rel="stylesheet">
    <link href="../style/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="../style/sweetalert.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../style/bootstrap-treeview.min.css">
    <script src="../js/env.js"></script>
    <style>
        .ibox-content {
            padding: 0;
        }

        /* .tab-content{
             border: 1px solid #F5F5F5;
         }*/
        .note-editable {
            /*height: 390px;*/
            overflow: auto;
            border: 1px solid #F5F5F5;
        }

        .ibox {
            margin: 0;
        }

        /*.tab-content{
            height: 500px;
        }*/
        .container {
            margin-top: 3%;
        }

        .panel-primary {
            border: none;
        }
        .note-toolbar {
            display: none;
        }
    </style>
</head>
<body style="background-color: #F5F5F5">
<div class="container">
    <div class="row">
        <!--<div class="col-md-2">
            <div class="form-group">
                <label for="input-search" >快捷搜索:</label>
                <input type="input" class="form-control" id="input-search" placeholder="请输入要查询的合同名称..." value="">
                <button type="button" class="btn btn-default" id="btn-clear-search">清除</button>
            </div>
        </div>-->
        <div class="col-md-4">
            <div class="panel panel-primary ">
                <div class="panel-body right_centent" style="overflow: auto;">
                    <div id="left-tree"></div>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="panel panel-primary ">
                <div class="panel-body right_centent">
                    <div class="form-group">
                        <p>选中节点:</p>
                        <input id="editName" type="text" class="form-control" autocomplete="off"/>
                    </div>
                    <div class="form-group"  style="text-align: right">
                        <button id="DelNode" class="btn">删除</button>
                        <button id="SaveNode" class="btn">添加节点</button>
                    </div>
                    <div class="tabs-container form-group">
                        <input name="typeId" hidden>
                        <input name="lawId" hidden>
                        <input name="parent" hidden>
                        <h4>合同相关法律法规</h4>
                        <ul class="nav nav-tabs">
                            <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">法律</a>
                            </li>
                            <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">行政法规</a>
                            </li>
                            <li class=""><a data-toggle="tab" href="#tab-3" aria-expanded="false">部门规章</a>
                            </li>
                            <li class=""><a data-toggle="tab" href="#tab-4" aria-expanded="false">司法解释</a>
                            </li>
                        </ul>
                        <div class="tab-content">
                            <div id="tab-1" class="tab-pane active">
                                <div class="ibox float-e-margins">
                                    <div class="ibox-content">
                                        <div class="summernote" style="padding: 120px;"></div>
                                    </div>
                                </div>
                            </div>
                            <div id="tab-2" class="tab-pane">
                                <div class="ibox float-e-margins">
                                    <div class="ibox-content">
                                        <div class="summernote" style="padding: 120px;"></div>
                                    </div>
                                </div>
                            </div>
                            <div id="tab-3" class="tab-pane">
                                <div class="ibox float-e-margins">
                                    <div class="ibox-content">
                                        <div class="summernote" style="padding: 120px;"></div>
                                    </div>
                                </div>
                            </div>
                            <div id="tab-4" class="tab-pane">
                                <div class="ibox float-e-margins">
                                    <div class="ibox-content">
                                        <div class="summernote" style="padding: 120px;"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group" style="text-align: center">
                        <button id="EditNode" class="btn-primary btn">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>
<script type="text/javascript" src="../js/bootstrap-treeview.js"></script>
<script src="../js/summernote.min.js"></script>
<script src="../js/summernote-zh-CN.js"></script>
<script src="../js/sweetalert.min.js"></script>
<script>
    function summernoteInit() {
        $('.summernote').summernote({
            lang: 'zh-CN',
            focus: true
        });
    }

    $(function () {
        var winHeight;
        if (window.innerHeight) {
            winHeight = window.innerHeight;
        } else if ((document.body) && (document.body.clientHeight)) {
            winHeight = document.body.clientHeight;
        }
        winHeight = winHeight * 0.9;
        $(".panel").css({"height": winHeight, "margin-bottom": 0});
        $(".panel-body").css({"height": winHeight * 0.96});
        $(".tabs-container").css({"height": winHeight * 0.70});

        summernoteInit();
        onLoad();
        $(".note-editable").css({"height": winHeight * 0.6});
        BindEvent();

        /* 搜索开始*/
        /*var findSearchableNodes = function() {
            return $('#left-tree').treeview('search', [ $.trim($('#input-search').val()), { ignoreCase: false, exactMatch: false } ]);
        };
        var searchableNodes = findSearchableNodes();
        // Select/unselect/toggle nodes
        $('#input-search').on('keyup', function (e) {
            var str = $('#input-search').val();
            if($.trim(str).length>0){
                searchableNodes = findSearchableNodes();
            } else {
                $('#treeview-searchable').treeview('collapseAll', {
                    silent : false //设置初始化节点关闭
                });
            }
            //$('.select-node').prop('disabled', !(searchableNodes.length >= 1));
        });
        var search = function(e) {
            var pattern = $.trim($('#input-search').val());
            /!*var options = {
                ignoreCase: $('#chk-ignore-case').is(':checked'),
                exactMatch: $('#chk-exact-match').is(':checked'),
                revealResults: $('#chk-reveal-results').is(':checked')
            };*!/
            var searchResult = $('#left-tree').treeview('search', [ pattern, null ]);
            if(searchResult.length==0){
                $('#left-tree').treeview('clearSearch');
                $('#left-tree').treeview('collapseAll', {
                    silent: false//设置初始化节点关闭
                });
            }
        };
        $('#input-search').on('keyup', search);
        $('#btn-clear-search').on('click', function (e) {
            $('#left-tree').treeview('clearSearch');
            $('#input-search').val('');
            $('#left-tree').treeview('collapseAll', {
                silent: false//设置初始化节点关闭
            });
        });*/
        /* 搜索结束*/
    });

    //页面加载
    function onLoad() {
        var tree = [{text: "正在初始化目录树。。"}];
        //渲染树
        $('#left-tree').treeview({
            data: tree,
            levels: 1
        });
        showTree();
    }
    function showTree() {
        $.post(requestUrl+"/contractType/showAllType", function (data) {
            $('#left-tree').treeview({
                data: data,
                levels: 1,
                onNodeSelected: function (event, node) {
                    $('#editName').val(node.text);
                    //  在这里添加隐藏表单字段
                    $("input[name=typeId]").val(node.typeId);
                    $.post(requestUrl+"/law/showOne", {"ContractId": node.typeId}, function (data) {
                        $("input[name=lawId]").val(data.lawId);
                        $("#tab-1 .note-editable").html(data.law);
                        $("#tab-2 .note-editable").html(data.statute);
                        $("#tab-4 .note-editable").html(data.judicial);
                        $("#tab-3 .note-editable").html(data.rules);
                    });
                },
                showCheckbox: false//是否显示多选
            });
        });
    }

    //事件注册
    function BindEvent() {
        //保存-新增   静态
         $("#SaveNode").click(function () {
             var parentNode = $('#left-tree').treeview('getSelected');
             var typeId = 0;
             if(parentNode.length>0){
                 typeId = parentNode[0].typeId;
             }
             $("input[name=parent]").val(typeId);
             var node = {
                 contractName: "新建节点",
                 parent: typeId
             };
             $.post("/contractType/addContractType", node, function (data) {
                 $('#left-tree').treeview('addNode', [data, parentNode]);
             });
             //showTree();
         });
        //保存-编辑
        $('#EditNode').click(function () {
            // 获取页面数据
            var parent = $("input[name=parent]").val();
            var contractId = $("input[name=typeId]").val();
            var contractName = $("#editName").val();
            if (contractId == "") {
                contractId = null;
            }
            if(parent==""){ parent = null; }
            if(contractName==""){ contractName = null; }
            var lawId = $("input[name=lawId]").val();
            if (lawId == "") {
                lawId = null;
            }
            if (contractId == null /*&& parent==null*/ && lawId == null) {
                swal({title: "错误", text: "请检查内容完整性", type: "error", closeOnConfirm: true});
            } else {
                var formData = {
                    parent : parent,
                    contractId: contractId,
                    lawId: $("input[name=lawId]").val(),
                    contractName : contractName,
                    law: $("#tab-1 .note-editable").html(),
                    statute: $("#tab-2 .note-editable").html(),
                    rules: $("#tab-3 .note-editable").html(),
                    judicial: $("#tab-4 .note-editable").html()
                };


                $.post(requestUrl+"/contractType/editContractType", formData, function (data) {
                    var node = $('#left-tree').treeview('getSelected');
                    var newNode = {
                        text: $('#editName').val(),
                        parent: data.contract.parent,
                        typeId: data.contract.typeId
                    };
                    $('#left-tree').treeview('updateNode', [node, newNode]);
                    $("input[name=lawId]").val(data.law.lawId);
                    swal({title: "完成", text: "法律法规内容提交成功", type: "success"/*, closeOnConfirm: true*/});
                });

              /*  $.post(requestUrl+"/law/editLaw", formData, function (data) {
                    if (data.editResult == true) {
                        swal({title: "完成", text: "法律法规内容提交成功", type: "success", closeOnConfirm: true});
                        $("input[name=lawId]").val(data.lawId);
                    }

                })*/
            }
        });
        $("#DelNode").click(function () {
            var node = $('#left-tree').treeview('getSelected');
            swal({
                title: "您确定要删除该节点吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的，我要删除！",
                cancelButtonText: "让我再考虑一下…",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function (isConfirm) {
                if (isConfirm) {
                    $.post(requestUrl+'/contractType/editContractType', {"contractId":$("input[name=typeId]").val(), "isused":"0"}, function (data) {
                        swal({title: "操作成功", text: "节点删除成功", type: "success", closeOnConfirm: false});
                        $('#left-tree').treeview('removeNode', [node, { silent: true }]);
                    });
                } else {
                    swal("已取消", "您取消了删除操作！", "error")
                }
            })

        });
    }


</script>
</body>
</html>